package com.dykj.dati.controller.h5;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.dykj.dati.config.RedisCache;
import com.dykj.dati.model.t.TKuTitle;
import com.dykj.dati.model.t.TiZong;
import com.dykj.dati.model.user.HUser;
import com.dykj.dati.service.user.IHUserService;
import com.dykj.dati.utils.ReturnUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

import static com.dykj.dati.utils.ServiceUtil.hasUser;

/**
 * @Author: yc
 * @Date: 2021/9/22 15:48
 */
@CrossOrigin
@Controller
@RequestMapping("/dth5")
public class KSController {
    public static List<TiZong> tizongList=new ArrayList<>();
    public static List<String> jsUserIds2=new ArrayList<>();//已参加竞赛的用户id
    public static Map<String,Date> tcMaps=new HashMap<>();
    public static Map<String,String> titleXuanxiang=new HashMap<>();
    public static Map<String, String> tizongMap=new HashMap<>();
    public static int tiNum=20;

    /**
     *
     * @param type  答题类型 0-练习，1-竞赛
     *
     * @return
     * 判断答题类型，竞赛只允许一次存储在内存list 返回数据时存储时间（map） 返回题和题词ID
     */
    @Autowired
    private RedisCache redisCache;
    @GetMapping("/ks")
    @ResponseBody
    public ModelMap ks(String type,String userId) {
        if(StringUtils.isBlank(userId)){
            return ReturnUtil.Error("请上传用户id");
        }
        if(! hasUser(userId)){
            return ReturnUtil.Error("非法访问");
        }


        //生成题次id并将获取题次的时间存入内存
        String tcId=IdUtil.simpleUUID();
       /* tcMap.put(tcId,new Date());*/
        redisCache.put(tcId,new Date());
        redisCache.expire(tcId,610);
        Map<String,Object> result=new HashMap<>();
        result.put("type",type);
        result.put("tcId",tcId);
        result.put("ti",getti());
        return ReturnUtil.Success("成功",result);
    }

    public List<TiZong>  getti(){
        List<TiZong> chouTi=new ArrayList<>();
        while(chouTi.size()<tiNum){
            int i= RandomUtil.randomInt(0,tizongList.size()-1);
            if(!chouTi.contains(tizongList.get(i))){
                chouTi.add(tizongList.get(i));
            }
        }

        return   chouTi;
    }
}
